package com.gugee.ins.common.dal.service.media.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.media.InsRelMediaCaptionTagMapper;
import com.gugee.ins.common.dal.service.media.InsRelMediaCaptionTagDao;
import com.gugee.ins.common.model.mysql.media.InsRelMediaCaptionTag;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * insBlogger dao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsRelMediaCaptionTagDaoImpl extends ServiceImpl<InsRelMediaCaptionTagMapper, InsRelMediaCaptionTag> implements InsRelMediaCaptionTagDao {

    /**
     * 批量插入
     * @param shortcode
     * @param tags
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(String shortcode,List<String> tags){
        long nowTime = System.currentTimeMillis() / 1000;

        List<InsRelMediaCaptionTag> insRelMediaCaptionTags = new ArrayList<>(tags.size());
        for(String tag : tags){
            InsRelMediaCaptionTag insRelMediaCaptionTag = new InsRelMediaCaptionTag();
            insRelMediaCaptionTag.setShortcode(shortcode);
            insRelMediaCaptionTag.setCaptionTag(tag);
            insRelMediaCaptionTag.setCreateTime(nowTime);
            insRelMediaCaptionTags.add(insRelMediaCaptionTag);
        }
        try{
            this.saveBatch(insRelMediaCaptionTags,insRelMediaCaptionTags.size() + 1);
        }catch (Exception e){
            log.debug("batch insert ins rel media caption tagger failed ,try insert one by one, msg : {} ",e.getMessage());
            insRelMediaCaptionTags.forEach(tag -> {
                try{
                    baseMapper.insert(tag);
                }catch (Exception ex){
                    log.info("insert ins rel media tag failed  ,shortcode : {},captionTag : {}, msg : {} "
                            ,shortcode
                            ,tag.getCaptionTag(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 根据uid 获取标题文本标记的内容话题
     * @param uid
     * @return
     */
    @Override
    public List<InsRelMediaCaptionTag> findCaptionTagByUid(long uid) {
        return baseMapper.findCaptionTagByUid(uid);
    }
}
